<?php
class ActionTopicsGroupsController extends AppController{
    
    public $uses = array('ActionTopicsGroup', 'ActionPost','ActionEvent');
    
    
    public function addAction($id){
        
        $user = $this->getCurrentUser();
        $idUser = $this->getCurrentUserId();
        
        $UserEnregistrer = $this->UserTopicGroup->find('first', array(
            'conditions' => array(
                'UserTopicGroup.users_id' =>  $idUser,
                'TopicsGroup.id' => $id
        )));
        
        $mongroup = $this->TopicsGroup->find('first', array(
            'conditions' => array(
                'TopicsGroup.id' => $id
        )));
        
        $data = NULL;
        
        if (!empty($mongroup) && !empty($UserEnregistrer)){
            $data = array('ActionTopicsGroup' => array(
                'topics_groups_id' => $id,
                'users_id' => $idUser,
                'created_on' => date('c')
            ));    
            $data = $this->ActionTopicsGroup->save($data);
        }
        else {
            $this->setFlash('Vous n\'êtes pas membre de ce groupe');
        }
        return $data;
    }
    
    
    public function addActionPost($id){
        
        if (!empty($this->request->data['ActionPost'])){
            
            $info = $this->addAction($id);
            $data  = $this->request->data['ActionPost'];
            $data['actions_id'] = $info['Action']['id'];
            $this->ActionPost->save($data);
            $this->Session->setFlash('Votre post a été enregistré');
            
        }
        $this->redirect(array('controller' => 'Groups', 'action' => 'view'.$id));
    }
}
?>
